package com.liuugoqing.creator.singleton.question.serializatio;

import java.io.Serializable;

/**
 * 序列化和反序列化时出现的问题
 *
 * @Author : liugq
 * @Date : 2022/6/28 11:39
 * @See : 参考类
 * @Since： JDK1.8
 * @Version : 1.0
 */
public class Singleton implements Serializable {
    private static volatile Singleton singleton = null;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (null == singleton) {
            synchronized (Singleton.class) {
                if (null == singleton)
                    singleton = new Singleton();
            }
        }
        return singleton;
    }

    /**
     * 解决单例模式序列化时的问题
     *
     * @return java.lang.Object
     * @author liugq
     * @date 2022/6/28 11:48
     */
    private Object readResolve() {
        return singleton;
    }

}
